//引入数据库
const db = require('../db/index')


//创建相册函数  参数：album_name、album_des、album_power
exports.createAlbum = (req, res) => {
    const albuminfo = req.body;
    // console.log(albuminfo);
    //根据当前用户创建一个属于自己的相册，还有权限 
    const insertSql = 'insert into album set ?'

    db.query(insertSql, { album_name: albuminfo.album_name, album_des: albuminfo.album_des, album_power: albuminfo.album_power, album_user_id: req.user.user_id, album_url: albuminfo.album_url, album_type: albuminfo.album_type }, (err, results) => {

        if (err) return res.cc(err)
        if (results == 0) return res.cc("创建相册失败!")

        res.send({
            status: 0,
            message: "创建相册成功",
            data: results
        })
    })
}

//创建搜索相册函数  参数：album_name
exports.searchAlbum = (req, res) => {

    const selectSql = 'select * from album where album_name like ?;'

    db.query(selectSql, req.query.value, (err, results) => {
        if (err) return res.cc(err)

        res.send({
            status: 0,
            message: "查询成功",
            data: results
        })

    })

}

//查询该用户下的所有相册信息
exports.showUserAlbums = (req, res) => {


    const selectSql = 'select username,user_url,album_name,album_num,album_type,album_des,album_url,album_power from user,album where album_user_id = user_id and user_id=?'

    db.query(selectSql, req.user.user_id, (err, results) => {


        if (err) return res.cc(err)

        if (results.length == 0) return res.cc("没有查询到相册信息")

        res.send({
            status: 0,
            message: '成功查询到该用户的所有相册',
            data: results
        })
    })
}